package com.bee.剑指offer;

import java.util.ArrayList;
import java.util.List;

/**
 * 输入一颗二叉树的根节点和一个整数，按字典序打印出二叉树中结点值的和为输入整数的所有路径。
 * 路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。
 *
 * @Author yong.Mr
 * @data 2021-04-23
 * @version 1.0.0
 */
public class 二叉树中和为某一值的路径 {

    private static final ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();

    private static final ArrayList<Integer> list = new ArrayList<Integer>();

    public static ArrayList<ArrayList<Integer>> FindPath(TreeNode root, int target) {
        if(root == null) {
            return result;
        }
        target -= root.val;
        list.add(root.val);
        if(target == 0 && root.left == null && root.right == null){
            result.add(new ArrayList<Integer>(list));
        }
        FindPath(root.left,target);
        FindPath(root.right,target);
        list.remove(list.size() - 1);
        return result;
    }

    private static final List<List<Integer>> res = new ArrayList<>();

    private static final List<Integer> temp = new ArrayList<>();

    public static List<List<Integer>> findPa(TreeNode root, int target) {
        if(root == null) {
            return res;
        }
        target -= root.val;
        temp.add(root.val);
        if(target == 0 && root.left == null && root.right == null) {
            res.add(new ArrayList<>(temp));
        }
        findPa(root.left,target);
        findPa(root.right,target);
        temp.remove(temp.size() - 1);
        return res;
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(10);
        root.left = new TreeNode(5);
        root.left.left = new TreeNode(4);
        root.left.right = new TreeNode(7);
        root.right = new TreeNode(12);
        ArrayList<ArrayList<Integer>> result = FindPath(root, 15);
        System.out.println(result);
    }
}
